import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';

import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../../public/serve/init/init.service';
import { TreeNode } from '../../../../public/common/api';
import { Service } from '../../../../server/server';

import { appProductUrl } from '../../../product.url';
import { product,exportRefreshTable } from '../../../product.data';
import {appSynonymUrl} from '../../../../synonym/synonym.url';
declare var $: any;
@Component({
	selector: 'app-product-indexing-task-content-table',
	templateUrl: './product-indexing-task-content-table.component.html',
	styleUrls: ['./product-indexing-task-content-table.component.css']
})
export class ProductIndexingTaskContentTableComponent implements OnInit {
	taskId: string = '';
	taskType:any;
	completeStatus: string;
	child:boolean;

	total: number = 0;
	naturalNum: number = 0;
	tabVal: Array<object>;

	isSelectAll: boolean = false;
	isChose: boolean = false;

    right: any = {
        "taskPermissionContent":{},
        "productTaskContent": {},
		"internalTaskContent": {},
		"themeTaskContent": {}
    };

	constructor(
		private activatedRoute: ActivatedRoute,
		private server: Service,
		public request: CudTableService,
		private tableListService: CudTableService,
		private hintsService: HintsService,
		private removeTableService: RemoveTableService,
		public initService: InitService,
	) {
		this.right = $.extend(this.right, server.getRight());
	 }

	ngOnInit() {
		this.activatedRoute.queryParams.subscribe((p: Params) => {
			 this.taskId = p['taskId'];
			 this.taskType = product.retrieval.taskType =  String(p['taskType']);
			 this.completeStatus = p['completeStatus'];
			 this.child = Boolean(Number( p['child']));

			//权限
            if(this.taskType == '1'){
                //内文任务
                this.right['taskPermissionContent'] = this.right['internalTaskContent'];
            }else if(this.taskType == '0'){
                //产品任务
                this.right['taskPermissionContent'] = this.right['productTaskContent'];
			}else if(this.taskType == '2'){
                //主题词任务
                this.right['taskPermissionContent'] = this.right['themeTaskContent'];
			}
			
			this.getTaskProductList();
		});

		this.exportRefreshTableEmit = exportRefreshTable.subscribe((value: any) => {
			if(value){
				this.getTaskProductList();
			}
		})
	}

	//取消订阅
	public exportRefreshTableEmit: any;
	ngOnDestroy() {
	  if (this.exportRefreshTableEmit) {
		this.exportRefreshTableEmit.unsubscribe()
	  }
	}

	/**
	 * 任务内容列表
	 */
	getTaskProductList() {
		let params = this.getTaskProductListParams();
		this.request.postRequest(this.server.baseUrl +appProductUrl.getTaskProductList, params).then((res: any) => {
			if (res.result_code === '0000') {
				this.tabVal = res.result;
				this.total = res.length;
				this.naturalNum = product.pagination.naturalNum;
			} else {
				this.hintsService.messages(res.message, res.result_code);
			}

			this._cancelChecked();
			return;
		}).catch((err: any) => {
			this.hintsService.messages("error", "error");
			this._cancelChecked();
			return;
		});
	}

	//判断当前用户是否是标引人
    _isTaskAssignBy(idxTaskAssignList){
		if(idxTaskAssignList){
			for(let i=0;i<idxTaskAssignList.length;i++)
			{
				if(idxTaskAssignList[i]['taskAssignBy'] == this.server.getuserName())
					return true;
			}
		}
		return false;
    }

	//查询参数
	private getTaskProductListParams() {
		return {
			token: product.retrieval.token,
			pageNo: product.pagination.pageNumber,
			pageSize: product.pagination.pageSize,
			taskId: this.taskId,
			queryCriteria: product.retrieval.queryCriteria
		}
	}


	/**
	 * 全选
	 * @param bool
	 * @param tabVal
	 */
	_selectAll(bool, tabVal) {
		this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'productId');
		if (this.isSelectAll == false) {
			this.getTaskProductList();
		}
	}

	/**
	 * 单选
	 * @param bool
	 * @param productId
	 * @param length
	 */
	_chose(bool, productId, length) {
		this.isSelectAll = this.removeTableService.removeChose(bool, productId, length)['bool'];
	}

	/**
	 * 重置表格复选框、选中的ID
	 */
	public _cancelChecked() {
		this.isChose = this.isSelectAll = false;
		this.removeTableService._clearId("productId");
		this.isSelectAll = false;
	}

	public eport(v){
		
		if(v.status == 0){
			this.hintsService.messages("标引还未完成，不能审核!", '9999');
		}else{
			this.initService._init(1);
			$("#productTastContentAudit").modal("show");
		}
	}

	public del(v){
		if(v.checkStatus == 200){
			this.hintsService.messages("标引已审核，不能删除!", '9999');
		}else{
			$("#productTastContentDelete").modal("show");
			this.initService._init(1);
		}
	}
}
